package com.sgb.member.view_model;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sgb.member.MemberConfig;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberTenderBean;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class ZhaoCaiViewModel extends ViewModel {
    public MutableLiveData<Boolean>  isLoadDialogLiveData = new MutableLiveData();
    public MutableLiveData<List<CommonPopEntity>>  allTabDataLiveData = new MutableLiveData();
    public MutableLiveData<Boolean>  isEmptyLiveData = new MutableLiveData();
    public MutableLiveData<List<MemberTenderBean>>  listLiveData = new MutableLiveData();
    public MutableLiveData<String>  webUrlLiveData = new MutableLiveData();
    public MutableLiveData<String>  msgLiveData = new MutableLiveData();
    public MutableLiveData<String>  startPickNoticeDetailLiveData = new MutableLiveData();
    public MutableLiveData<String>  startTenderDetailsLiveData = new MutableLiveData();


    // 招标类型 (商品材料、专业工程、劳务分包...)
    public void getTenderNewSubTypeList() {
        isLoadDialogLiveData.postValue(true);
        MemberConfig.getInstance().getApi().tenderNewTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {

            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().set(0, new CommonPopEntity("", "全部"));
                    allTabDataLiveData.postValue(data.getData());
                } else {
                    allTabDataLiveData.postValue(new ArrayList<>());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CommonPopEntity>>> call, Throwable t) {
                isLoadDialogLiveData.postValue(false);
                allTabDataLiveData.postValue(new ArrayList<>());
            }

            @Override
            public void onFinally() {
                isLoadDialogLiveData.postValue(false);
            }
        });
    }

    private int current;

    //招标大厅-全部
    public void findTenderingHomeEsPage(boolean isLoadMore, String mNo1) {
        int currentPage = isLoadMore ? current + 1 : 1;
        isLoadDialogLiveData.postValue(true);
        Map<String, Object> map = new HashMap<>();
        map.put("page", currentPage);
        map.put("current", currentPage);
        map.put("limit", 10);
        map.put("keyword", "");
        map.put("type", mNo1);
        map.put("goodsTypeNo", "");
        map.put("verticalType", "");
        map.put("province", "");
        map.put("city", "");
        MemberConfig.getInstance().getApi().tenderAll(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberTenderBean>>>>() {

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberTenderBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if (!isLoadMore) {
                    isEmptyLiveData.postValue(true);
                }
            }

            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberTenderBean>>> data) {
                DialogHelper.dismissProgressDialog();
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                listLiveData.postValue(data.getData().getList());
            }

            @Override
            public void onFinally() {
                isLoadDialogLiveData.postValue(false);
            }
        });
    }


    public void tenderItemClick(MemberTenderBean memberTenderBean) {
        if (memberTenderBean.getBusinessType() == 2) { //1.招标； 2.竞价
            getUrlSetting("competitivePurchase", "?id=" + memberTenderBean.getId());
        } else if (memberTenderBean.getBusinessType() == 1) {
            if (memberTenderBean.getState() == 8) {
                startPickNoticeDetailLiveData.postValue(memberTenderBean.getComNo());
            } else {
                startTenderDetailsLiveData.postValue(memberTenderBean.getId());

            }
        } else if (memberTenderBean.getBusinessType() == 3) {
            boolean saveBooleanData = SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false);
            String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
            String longitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
            String latitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
            if (TextUtils.isEmpty(longitude)) {
                longitude = "0";
            }
            if (TextUtils.isEmpty(latitude)) {
                latitude = "0";
            }
            String endUrl = "?id=" + memberTenderBean.getId() + "&lng=" + longitude + "&lat=" + latitude + "&type=" + memberTenderBean.getModuleType()+"&isDpprove"+saveBooleanData +"&companyNo"+companyNo;
            getUrlSetting("engineeringProject", endUrl);
        }
    }

    public void getUrlSetting(String param, String endUrl) {
        isLoadDialogLiveData.postValue(true);
        MemberConfig.getInstance().getApi().getUrlSetting("competitivePurchase").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + endUrl;
                    webUrlLiveData.postValue(url);

                } else {
                    msgLiveData.postValue(data.getMsg());
                }
            }
        });
    }
}
